<?php

/**
 *    资金交易支付网关通知接口
 *
 *    @author    censhuixing
 *    @time   2014-05-14
 */
class NotifyApp extends MallbaseApp
{
	//同步跳转
	function index()
	{
		//这里是支付宝，财付通等当订单状态改变时的通知地址
		$code   = isset($_GET['code']) ? trim($_GET['code']) : 0; //哪个支付方式
		if (!$code)
		{
			/* 无效的通知请求 */
			$this->show_warning('no_such_payment','联系客服','index.php');

			exit;
		}

		/* 调用相应的支付方式 */
		$file = ROOT_PATH."/includes/payments/{$code}.php";
		if(file_exists($file))
		{
			include_once($file);
			$payment = new $code();
		}
		else
		{
			/* 无效的通知请求 */
			$this->show_warning('no_such_payment','联系客服','index.php');
			exit;
		}

		/* 获取验证结果 */
		$result=$payment->respond();

		if ($result === false)
		{
			/* 支付失败 */
			$this->show_warning('支付失败','联系客服','index.php');
			exit;
		}
		else
		{

			#TODO 临时在此也改变订单状态为方便调试，实际发布时应把此段去掉，订单状态的改变以notify为准
			//$this->_change_order_status($order_id, $order_info['extension'], $notify_result);

			/* 只有支付时会使用到return_url，所以这里显示的信息是支付成功的提示信息 */
			$this->_curlocal('充值成功');
			$this->assign('total_fee', $_GET['total_fee']);
			$this->display('capital_recharge_notify.html');
		}
	}

	/**
     *    支付完成后，外部网关的通知地址，在此会进行订单状态的改变，这里严格验证，改变订单状态
     *
     *    @author    Garbin
     *    @return    void
     */
	function  async()
	{
		//这里是支付宝，财付通等当订单状态改变时的通知地址
		//这里是支付宝，财付通等当订单状态改变时的通知地址
		$code   = isset($_GET['code']) ? trim($_GET['code']) : 0; //哪个支付方式
		if (!$code)
		{
			/* 无效的通知请求 */
			$this->show_warning('no_such_payment','联系客服','index.php');

			exit;
		}

		/* 调用相应的支付方式 */
		$file = ROOT_PATH."/includes/payments/{$code}.php";
		if(file_exists($file))
		{
			include_once($file);
			$payment = new $code();
		}
		else
		{
			/* 无效的通知请求 */
			$this->show_warning('no_such_payment','联系客服','index.php');
			exit;
		}

		/* 获取验证结果 */
		$result=$payment->respond();

		if ($result === false)
		{
			/* 支付失败 */
			$this->show_warning('支付失败','联系客服','index.php');
			exit;
		}

		/*----------通知验证结束----------*/
		/*----------本地验证开始----------*/
		else
		{

			//获取订单信息
			$trans_code = trim($_REQUEST['out_trade_no']);
			$model_trans =& m('user_trans');
			$order_info  = $model_trans->get_info_by_trans_code($trans_code);

			if (empty($order_info))
			{
				//没有该订单
				$this->show_warning('forbidden','联系客服','index.php');

				return;
			}
			if ($order_info['trans_amount'] != $_GET['total_fee'])
			{
				/* 支付的金额与实际金额不一致 */
				$this->show_warning('支付的金额与实际金额不一致','联系客服','index.php');

				return;
			}
			//改变订单状态
			if($order_info['trans_instatus'] == 0)
			{

				$data['trans_instatus'] =  1;
				$data['trans_intime'] =  time();
				$row  = $model_trans->edit("trans_code='{$trans_code}'",$data);
				if($row)
				{

					$user_id = $order_info['trans_uid'];
					$model_usermoney =& m('usermoney');
					$usermoney_info  = $model_usermoney->get_info_by_user_id($user_id);

					if (empty($usermoney_info))
					{
						$user_money['user_id'] = $user_id;
						$user_money['user_money'] = $order_info['trans_amount'];
						$model_usermoney->add($user_money);
					}
					else
					{
						$user_money['user_id'] = $user_id;
						$user_money['user_money'] = $usermoney_info['user_money']+$order_info['trans_amount'];
						$model_usermoney->edit('user_id='.$user_id,$user_money);
					}
					//记录入志
					$account_log = array();
					$account_log['user_id'] = $user_id;
					$account_log['order_no'] = $trans_code;
					$account_log['user_money'] = $order_info['trans_amount'];
					$account_log['change_time'] = time();
					$account_log['change_desc'] = '账户现金充值';
					$account_log['change_type'] = 0;
					$model_accountlog =& m('accountlog');
					$model_accountlog->add($account_log);

				}


			}

			//提示成功
			$this->_curlocal('充值成功');
			$this->assign('total_fee', $_GET['total_fee']);
			$this->display('capital_recharge_notify.html');


		}


	}


}

?>
